package com.tvunetworks.center.device.util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
/**
 * @program:
 * @description:
 * @author HenryChen
 * @create: 9/27/2019 2:33 PM
 **/
@Component
public class ApplicationContextProvider implements ApplicationContextAware {
	/**
	 * 上下文对象实例
	 */
	private static ApplicationContext APPLICATIONCONTEXT;

	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		setApplicationContexts(applicationContext);
	}



	/**
	 * 为了符合代码规范,使用静态方法赋值
	 * @param applicationContext 参数
	 * @throws BeansException 编译异常
	 */
	public  static  void setApplicationContexts(ApplicationContext applicationContext) throws BeansException {
		ApplicationContextProvider.APPLICATIONCONTEXT = applicationContext;
	}
	/**
	 * 获取applicationContext
	 * 
	 * @return ApplicationContext
	 */
	public static ApplicationContext getApplicationContext() {
		return APPLICATIONCONTEXT;
	}

	/**
	 * 通过name获取 Bean.
	 * 
	 * @param name name
	 * @return Object
	 */
	public static Object getBean(String name) {
		return getApplicationContext().getBean(name);
	}

	/**
	 * 通过class获取Bean.
	 * 
	 * @param clazz clazz
	 * @param <T> t
	 * @return  getBean
	 */
	public static <T> T getBean(Class<T> clazz) {
		return getApplicationContext().getBean(clazz);
	}

	/**
	 * 通过name,以及Clazz返回指定的Bean
	 *
	 * @param name name
	 * @param clazz clazz
	 * @param <T> t
	 * @return getBean
	 */
	public static <T> T getBean(String name, Class<T> clazz) {
		return getApplicationContext().getBean(name, clazz);
	}
}